<nz-card>
  <nz-descriptions
    nzTitle="基本信息"
    nzBordered
    nzSize="small"
    [nzColumn]="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }"
  >
    <nz-descriptions-item nzTitle="单号">
      {{ detailsData?.warehousingNumber || '-' }}
      <i
        *ngIf="detailsData.warehousingNumber"
        nz-icon
        nzType="copy"
        nzTheme="outline"
        nz-tooltip
        nzTooltipTitle="点击复制"
        (click)="coprText(detailsData.warehousingNumber)"
      ></i>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="状态">
      <span [ngStyle]="{'color': detailsData.state | storePutStatus: 'color'}">
        {{ detailsData.state | storePutStatus: 'label' }}
      </span>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="入库类型">
      <span>
        {{ filterText(putTypeOptions, detailsData.type, 'content') }}
      </span>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="预计到货日期">{{ detailsData?.arrivalTime || '-' }}</nz-descriptions-item>

    <nz-descriptions-item nzTitle="入库仓库">
      {{ filterText(warehouseOptions, detailsData.whPutId, 'id') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="入库品质">
      <span>
        {{ detailsData.quality === 0 ? '不良品' : (detailsData.quality === 1 ? '良品' : '-') }}
      </span>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="供应商" nzSpan="2">
      {{ filterText(supplierOptions, detailsData.supId, 'id') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="备注" nzSpan="4">
      {{ detailsData.remark || '-' }}
    </nz-descriptions-item>
  </nz-descriptions>
</nz-card>

<nz-card class="m-t-4">
  <p class="detail-title">入库明细</p>

  <div class="table-wrap">
    <nz-table
      #mxTable
      nzBordered
      nzSize="small"
      [nzShowPagination]="false"
      [nzFrontPagination]="false"
      [nzData]="warehousTableData">
      <thead>
        <tr>
          <th nzAlign="center" rowspan="2" nzWidth="70px">序号</th>
          <th nzAlign="center" colspan="3">商品信息</th>
          <th nzAlign="center" colspan="3" *ngIf="detailsData.type === 1">采购信息</th>
          <th nzAlign="center" colspan="4">入库信息</th>
          <th nzAlign="center" rowspan="2">操作</th>
        </tr>
        <tr>
          <th nzAlign="center">商品编号</th>
          <th nzAlign="center">商品名称</th>
          <th nzAlign="center">商品69码</th>

          <ng-container *ngIf="detailsData.type === 1">
            <th nzAlign="center">采购数量</th>
            <th nzAlign="center">冻结数量</th>
            <th nzAlign="center">已入库数量</th>
          </ng-container>

          <th nzAlign="center">行状态</th>
          <th nzAlign="center">预计入库数量</th>
          <th nzAlign="center">实际入库数量</th>
          <th nzAlign="center">入库库位</th>
        </tr>
      </thead>
      <tbody>
        <tr
          *ngFor="let data of mxTable.data; let index = index;"
          [ngClass]="{'tr-bg-red': data?.lineState === 1}">
          <!-- 序号 -->
          <td nzAlign="center">{{ index + 1 }}</td>
          <!-- 商品编号 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsCode || '-' }}
            </span>
          </td>
          <!-- 商品名称 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsName || '-' }}
            </span>
          </td>
          <!-- 商品69码 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsTwoCode || '-' }}
            </span>
          </td>

          <ng-container *ngIf="detailsData.type === 1">
            <!-- 采购数量 -->
            <td nzAlign="center">
              <span [ngStyle]="{'color': data?.purchaseNumber !== data?.stockNumber ? '#FF9900' : '' }">
                {{ data?.purchaseNumber || '0' }}
              </span>
            </td>
            <!-- 冻结数量 -->
            <td nzAlign="center">
              <span [ngStyle]="{'color': data?.purchaseNumber !== data?.stockNumber ? '#FF9900' : '' }">
                {{ data?.frozenNumber || '0' }}
              </span>
            </td>
            <!-- 已入库数量 -->
            <td nzAlign="center">
              <span [ngStyle]="{'color': data?.purchaseNumber !== data?.stockNumber ? '#FF9900' : '' }">
                {{ data?.stockNumber || '0' }}
              </span>
            </td>
          </ng-container>

          <!-- 行状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data?.lineState === 0 ? '#67C23A' : '#F56C6C'}">
              {{ data?.lineState === 0 ? '正常' : (data?.lineState === 1 ? '已作废' : '-') }}
            </span>
          </td>
          <!-- * 预计入库数量 -->
          <td nzAlign="center">
            {{ data?.number || '0' }}
          </td>
          <!-- 实际入库数量 -->
          <td nzAlign="center">
            <ng-container *ngIf="data?.lineState === 1; else elseSjrTemplate">
              <!-- 作废 -->
              -
            </ng-container>
            <ng-template #elseSjrTemplate>
              <ng-container [ngSwitch]="data.type">
                <!-- SN管控 -->
                <span *ngSwitchCase="1">
                  <span style="color: #909399;">
                    {{ data.actualStorageNumber ? '已录入' + data.actualStorageNumber + '个SN' : '请录入SN' }}
                  </span>
                </span>
                <!-- 默认 libraryText -->
                <div *ngSwitchDefault>
                  <ng-container *ngIf="data.type === 2 || data?.libraryText; else elseTemplate">
                    <!-- 多库位 -->
                    <span style="color: #909399;">
                      {{ libraryTextStr(data?.libraryText) }}
                    </span>
                  </ng-container>
                  <ng-template #elseTemplate>
                    <nz-input-number
                      [nzMin]="1"
                      [nzStep]="1"
                      [nzPrecision]="0"
                      nzPlaceHolder="请输入"
                      [nzDisabled]="detailsData.state === 4"
                      [(ngModel)]="data.actualStorageNumber"
                    ></nz-input-number>
                  </ng-template>
                </div>
              </ng-container>
            </ng-template>
          </td>
          <!-- 入库库位 -->
          <td nzAlign="center">
            <ng-container *ngIf="data?.lineState === 1; else elseRkTemplate">
              <!-- 作废 -->
              -
            </ng-container>
            <ng-template #elseRkTemplate>
              <ng-container [ngSwitch]="data.type">
                <!-- SN管控 -->
                <span *ngSwitchCase="1">
                  <span style="color: #909399;">录入SN时选择</span>
                </span>
                <!-- 多库位 -->
                <span *ngSwitchCase="2">
                  -
                </span>
                <!-- 默认 libraryText -->
                <div *ngSwitchDefault>
                  <ng-container *ngIf="data.libraryText; else elseKwsTemplate">
                    -
                  </ng-container>
                  <ng-template #elseKwsTemplate>
                    <nz-select
                      style="width: 150px; font-weight: normal;"
                      nzShowSearch
                      nzAllowClear
                      nzPlaceHolder="选择库位"
                      [nzDisabled]="detailsData.state === 4"
                      [(ngModel)]="data.libraryId">
                      <nz-option
                        *ngFor="let option of locationOptions"
                        [nzLabel]="option.name"
                        [nzValue]="option.id"
                      ></nz-option>
                    </nz-select>
                  </ng-template>
                </div>
              </ng-container>
            </ng-template>
          </td>
          <!-- 操作 -->
          <td nzAlign="center">
            <ng-container *ngIf="data?.lineState === 1; else elseCzTemplate">
              <!-- 作废 -->
              -
            </ng-container>
            <ng-template #elseCzTemplate>
              <button
                *ngIf="permission.userPermission.has('storageCenter:put:handle') && data.type === 1 && detailsData.state === 3"
                nz-button
                nzType="link"
                nzSize="small"
                (click)="showSnInfoModal(data)"
              >批量录入SN</button>

              <button
                *ngIf="permission.userPermission.has('storageCenter:put:handle') && ![0, 1].includes(data.type) && detailsData.state === 3"
                nz-button
                nzType="link"
                nzSize="small"
                [disabled]="data.actualStorageNumber > 0 && !data.libraryText"
                (click)="showMoreLocationModal(data)"
              >多库位入库</button>
            </ng-template>
          </td>
        </tr>

        <tr
          *ngIf="warehousTableData?.length"
          style="background: #fafafa; font-weight: 600;">
          <td nzAlign="center" colSpan="4">合计</td>

          <ng-container *ngIf="detailsData.type === 1">
            <!-- 采购 -->
            <td nzAlign="center">{{ calcSumTotal('purchaseNumber') }}</td>
            <!-- 冻结 -->
            <td nzAlign="center">{{ calcSumTotal('frozenNumber') }}</td>
            <!-- 已入库 -->
            <td nzAlign="center">{{ calcSumTotal('stockNumber') }}</td>
          </ng-container>
          
          <td nzAlign="center">-</td>
          <!-- 预计入库数 -->
          <td nzAlign="center">{{ calcSumTotal('number') }}</td>
          <!-- 实际入库数 -->
          <td nzAlign="center">{{ calcSumTotal('actualStorageNumber') }}</td>
          <td nzAlign="center">
            <nz-select
              style="width: 150px; font-weight: normal;"
              nzShowSearch
              nzAllowClear
              ngModel
              nzPlaceHolder="批量选择库位"
              [nzDisabled]="detailsData.state === 4"
              (ngModelChange)="batchLocationChange($event)">
              <nz-option
                *ngFor="let option of locationOptions"
                [nzLabel]="option.name"
                [nzValue]="option.id"
              ></nz-option>
            </nz-select>
          </td>
          <td nzAlign="center">-</td>
        </tr>
      </tbody>
    </nz-table>
  </div>
</nz-card>

<nz-card class="m-t-4">
  <div nz-row nzJustify="center">
    <button
      *ngIf="permission.userPermission.has('storageCenter:put:handle') && detailsData.state === 3"
      nz-button
      nzType="primary"
      class="m-r-10"
      [nzLoading]="submitLoading"
      (click)="submitHandlePut()"
    >暂存</button>
    
    <button
      *ngIf="permission.userPermission.has('storageCenter:put:handle') && detailsData.state === 3"
      nz-button
      nzType="primary"
      class="mbtn-orange m-r-10"
      (click)="warehousOver()"
    >入库完毕</button>

    <button
      nz-button
      nzType="default"
      (click)="pageBack()"
    >返回</button>
  </div>
</nz-card>

<!-- S SN信息 -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [nzWidth]="1000"
  [(nzVisible)]="isSninfoVisible"
  [nzTitle]="'SN信息'"
  (nzOnCancel)="isSninfoVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div>
        <button
          nz-button
          nzType="primary"
          class="m-r-10"
          [nzLoading]="subSnLoading"
          (click)="submitSnInfoData()"
        >确认</button>

        <button
          nz-button
          nzType="primary"
          class="mbtn-green"
          (click)="showFastInputModal()"
        >快速录入</button>
      </div>
      
      <nz-divider nzDashed></nz-divider>

      <nz-descriptions [nzColumn]="2">
        <nz-descriptions-item nzTitle="商品编号">{{ editGoodsItem?.whGoodsCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="69码">{{ editGoodsItem?.whGoodsTwoCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="商品名称">{{ editGoodsItem?.whGoodsName }}</nz-descriptions-item>
      </nz-descriptions>

      <nz-table
        #addSnTable
        nzSize="small"
        nzBordered
        [nzFrontPagination]="false"
        [nzFooter]="footerAddTmp"
        [nzShowPagination]="false"
        [nzData]="setSnData">
        <thead>
          <tr>
            <th nzAlign="center" nzWidth="80">序号</th>
            <th nzAlign="center">
              库位
              <a
                class="m-l-20"
                nz-dropdown
                [nzDropdownMenu]="menu">
                <i nz-icon nzType="caret-down" nzTheme="outline"></i>
              </a>
              <nz-dropdown-menu #menu="nzDropdownMenu">
                <ul nz-menu nzSelectable class="kjul-wrap">
                  <li
                    *ngFor="let option of locationOptions"
                    nz-menu-item
                    (click)="thQuickChange(option)"
                  >{{ option.name }}</li>
                </ul>
              </nz-dropdown-menu>
            </th>
            <th nzAlign="center">SN码</th>
            <th nzAlign="center">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let data of addSnTable.data; let index = index;">
            <td nzAlign="center">{{ index + 1 }}</td>
            <td nzAlign="center">
              <nz-select
                style="width: 220px;"
                nzShowSearch
                nzAllowClear
                nzPlaceHolder="请选择库位"
                [(ngModel)]="data.libraryId"
                (ngModelChange)="locationChange($event, index, setSnData)">
                <nz-option
                  *ngFor="let option of locationOptions"
                  [nzLabel]="option.name"
                  [nzValue]="option.id"
                ></nz-option>
              </nz-select>
            </td>
            <td nzAlign="center">
              <input
                style="width: 220px;"
                id="{{ 'snInput' + index }}"
                nz-input
                placeholder="请输入或扫描"
                [(ngModel)]="data.code"
                onkeyup="this.value=this.value.replace(/[^\w_/]/g,'');"
                (keydown)="snCodeKeDowm($event, index, 'snInput')"
              />
            </td>
            <td nzAlign="center">
              <i
                nz-icon
                nzType="delete"
                nzTheme="outline"
                class="delete-btn"
                (click)="deleteSnItem(index)"
              ></i>
            </td>
          </tr>
        </tbody>
      </nz-table>

      <ng-template #footerAddTmp>
        <div nz-row nzJustify="center">
          <button
            nz-button
            nzType="link"
            nzSize="small"
            (click)="addSnLineData()">
            <i nz-icon nzType="plus" nzTheme="outline"></i>
            添加行
          </button>
        </div>
      </ng-template>
    </div>
  </div>
</nz-modal>
<!-- E SN信息 -->

<!-- S 快速录入 -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isFastVisible"
  [nzTitle]="'快速录入'"
  (nzOnCancel)="isFastVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div class="p-b-10">
        <button
          nz-button
          nzType="primary"
          (click)="submitFastValue()"
        >确认</button>
        <span class="fast-tip-t">请将待录入的SN信息粘贴到下面输入框中</span>
        <i
          nz-icon
          nzType="exclamation-circle"
          nzTheme="outline"
          class="m-l-10"
          nz-tooltip
          [nzTooltipTitle]="fastTipTemplate"
        ></i>
        <ng-template #fastTipTemplate>
          <!-- <p>输入格式，例：库位,SN码</p>
          <p>库位与SN码间使用,(英文逗号符)分割</p> -->
          <p>注意：单行代表一个SN码</p>
        </ng-template>
      </div>

      <textarea
        rows="8"
        nz-input
        placeholder="每行为1个SN信息，请将待录入的SN信息粘贴到这里"
        [(ngModel)]="fastTextareaVal"
      ></textarea>
    </div>
  </div>
</nz-modal>
<!-- E 快速录入 -->

<!-- S 多库位 --> 
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [nzWidth]="1000"
  [(nzVisible)]="isMoreLocationVisible"
  [nzTitle]="'多库位'"
  (nzOnCancel)="isMoreLocationVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div>
        <button
          nz-button
          nzType="primary"
          (click)="submitSetLocation()"
        >确认</button>

        <!-- <button
          nz-button
          nzType="primary"
          nzDanger
          class="m-l-10"
          [disabled]="!setLocationData.length"
          (click)="clearSetLocation()"
        >清空所选</button> -->
      </div>
      
      <nz-divider nzDashed></nz-divider>

      <nz-descriptions [nzColumn]="2">
        <nz-descriptions-item nzTitle="商品编号">{{ editGoodsItem?.whGoodsCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="69码">{{ editGoodsItem?.whGoodsTwoCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="商品名称">{{ editGoodsItem?.whGoodsName }}</nz-descriptions-item>
      </nz-descriptions>

      <nz-table
        #dkwTable
        nzSize="small"
        nzBordered
        [nzFrontPagination]="false"
        [nzFooter]="addLineTmp"
        [nzShowPagination]="false"
        [nzData]="setLocationData">
        <thead>
          <tr>
            <th nzAlign="center" nzWidth="80">序号</th>
            <th nzAlign="center">库位</th>
            <th nzAlign="center">入库数量</th>
            <th nzAlign="center">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let data of dkwTable.data; let index = index;">
            <td nzAlign="center">{{ index + 1 }}</td>
            <td nzAlign="center">
              <nz-select
                style="width: 220px;"
                nzShowSearch
                nzAllowClear
                nzPlaceHolder="请选择库位"
                [(ngModel)]="data.id"
                (ngModelChange)="locationChange($event, index, setLocationData)">
                <nz-option
                  *ngFor="let option of locationOptions"
                  [nzLabel]="option.name"
                  [nzValue]="option.id"
                ></nz-option>
              </nz-select>
            </td>
            <td nzAlign="center">
              <nz-input-number
                style="width: 220px;"
                nzId="{{ 'numInput' + index }}"
                [nzMin]="1"
                [nzMax]="editGoodsItem?.number || 99999"
                nzPlaceHolder="请输入入库数量"
                [(ngModel)]="data.num"
                (keydown)="snCodeKeDowm($event, index, 'numInput')"
              ></nz-input-number>
            </td>
            <td nzAlign="center">
              <i
                nz-icon
                nzType="delete"
                nzTheme="outline"
                class="delete-btn"
                (click)="deleteKwItem(index)"
              ></i>
            </td>
          </tr>
        </tbody>
      </nz-table>

      <ng-template #addLineTmp>
        <div nz-row nzJustify="center">
          <button
            nz-button
            nzType="link"
            nzSize="small"
            (click)="addLocationLineItem()">
            <i nz-icon nzType="plus" nzTheme="outline"></i>
            添加行
          </button>
        </div>
      </ng-template>
    </div>
  </div>
</nz-modal>
<!-- E 多库位 -->